<?php

namespace App\Models\Game\Settlement;

use App\Models\Game\Model;
use App\Models\Game\Platform\Org;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Models\Elasticsearch\Settlement\Info as EsInfo;

class Info extends Model
{

    /**
     * 数据表名称
     *
     * @var string
     */
    protected $table = 't_settlement_info';

    /**
     * 进行类型转换的字段
     *
     * @var array
     */
    protected $casts = [
        'usd' => 'array',
        'ext' => 'array',
    ];

	const STATUS_NORMAL = 1;
    /**
     * 玩家类型【玩家】
     */
    const PLAYER_TYPE = 1;

    /**
     * 玩家类型【AI】
     */
    const PLAYER_TYPE_AI = 2;

    /**
     * 获得平台信息
     *
     * @return BelongsTo
     */
    public function org(): BelongsTo
    {
        return $this->belongsTo(Org::class, 'plat_code', 'code')->withTrashed();
    }

    /**
     * 获取游戏数据
     *
     * @param string $sn
     * @return array|null
     */
    protected function getData(string $sn): ?array
    {
        if (!config('elasticsearch.default')) {
            $data = $this->newQuery()->where('sn', $sn)->first();

            return $data ? $data->toArray() : null;
        } else {
            return EsInfo::getData($sn);
        }
    }

    /**
     * 获取去重统计字段列表
     *
     * @param int $timeStart
     * @param int $timeEnd
     * @param array $fields
     * @return \Illuminate\Support\Collection
     */
    protected function getStatsDistinct(int $timeStart, int $timeEnd, array $fields)
    {
        if (!!config('elasticsearch.default')) {
            return EsInfo::getStatsDistinct($timeStart, $timeEnd, $fields);
        }

        return $this->newQuery()->getQuery()->select($fields)
            ->where('end_time', '>=', $timeStart)
            ->where('end_time', '<', $timeEnd)
            ->distinct()
            ->get();
    }
}
